using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS
	/// channel: n/a
	/// command code: HN X Y
	/// created: 
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class CAUVANG
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public CAUVANG(SMS_MO MO)
		{
			WMO = MO;
		}

		public CAUVANG()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim();
			//string[] msg = tmpMsg.Split(new char[] { ' ' });
			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(tmpMsg);
			if(strReturnMsg!="")
			{
				#region Write text file
				string strcurrency="500";
				if(WMO.ServiceID.Replace("04","")=="6072")
					strcurrency="500";
				else if(WMO.ServiceID.Replace("04","")=="6172")
					strcurrency="1000";
				else if(WMO.ServiceID.Replace("04","")=="6272")
					strcurrency="2000";
				else if(WMO.ServiceID.Replace("04","")=="6372")
					strcurrency="3000";
				else if(WMO.ServiceID.Replace("04","")=="6472")
					strcurrency="4000";
				else if(WMO.ServiceID.Replace("04","")=="6572")
					strcurrency="5000";
				else if(WMO.ServiceID.Replace("04","")=="6672")
					strcurrency="10000";
				else if(WMO.ServiceID.Replace("04","")=="6772")
					strcurrency="15000";

				TextWriter twriter = new StreamWriter("Cauvang\\" + WMO.CommandCode.ToUpper() + "_" + WMO.MO_ID + ".txt");
				twriter.Write(tmpMsg + "," + WMO.UserID + "," + WMO.RequestTime + "," + strcurrency);
				twriter.Close();
				#endregion

				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;   
		}
		public string GetReturnMsg(string arr)
		{
			string naptien;
			string msgBody="";
			#region Currency
			if (WMO.ServiceID.Replace("04","")=="6072")
			{
				naptien="500";
			}
			else if (WMO.ServiceID.Replace("04","")=="6172")
			{
				naptien="1000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6272")
			{
				naptien="2000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6372")
			{
				naptien="3000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6472")
			{
				naptien="4000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6572")
			{
				naptien="5000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6672")
			{
				naptien="10000";
			}
			else if (WMO.ServiceID.Replace("04","")=="6772")
			{
				naptien="15000";
			}
			else
			{
				msgBody = ConfigSettings.ReadSetting("SMS_HELP").ToString();
				return msgBody;
			}
			#endregion
			Insert_CAUVANG_USERS(WMO.UserID, WMO.ServiceID, arr, naptien, WMO.RequestTime);
			msgBody = "Ban da nap thanh cong " + naptien + " vao tai khoan: " + arr;
			return msgBody;
		}
		private void Insert_CAUVANG_USERS(string _USERID, string _SERVICEID, string _USERNAME, string _CURRENCY, System.DateTime _REQUESTTIME)
		{
			string strSQLInsert="";
			strSQLInsert = "INSERT INTO CAUVANG(USERID, SERVICEID, USERNAME, CURRENCY, REQUESTTIME)";
			strSQLInsert += " VALUES(@USERID, @SERVICEID, @USERNAME, @CURRENCY, @REQUESTTIME)";
			SqlConnection cnn = new SqlConnection("server=115.84.178.97;uid=sa;pwd=hieutc;database=VIT_SEL");
			cnn.Open();

			SqlCommand cmd;
			cmd = new SqlCommand(strSQLInsert,cnn);			
			cmd.Parameters.Clear();

			cmd.Parameters.Add("@USERID", _USERID);
			cmd.Parameters.Add("@SERVICEID", _SERVICEID);
			cmd.Parameters.Add("@USERNAME", _USERNAME);
			cmd.Parameters.Add("@CURRENCY", _CURRENCY);	
			cmd.Parameters.Add("@REQUESTTIME", _REQUESTTIME);
					
			cmd.ExecuteNonQuery();
			cmd.Dispose();
			cnn.Close();				
		}
	}		
}
